package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.Game;
import com.javasm.entity.ReturnCode;
import com.javasm.service.impl.EditMenuServiceImpl;
import com.javasm.service.impl.GameServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author WangJie
 * @version 1.0
 * @Description:
 * @date: 2020/7/20 21:25
 * @since JDK 1.8
 */
@WebServlet("/editGame")
public class EditGameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        Map<String, Object> returnData = new HashMap<>();
        int result = 0;
        if ("insertGameInfo".equals(method)){
            result = insertGameInfo(req,resp);
        }else if ("deleteGameInfo".equals(method)){
            result = deleteGameInfo(req,resp);
        }else if ("updateGameInfo".equals(method)){
            result = updateGameInfo(req,resp);
        }
        // 操作是否成功的判断
        if (result >= 1) {
            returnData.put("returnCode", ReturnCode.SUCCESS.getCode());
            returnData.put("returnMsg", ReturnCode.SUCCESS.getMsg());
        } else {
            returnData.put("returnCode", ReturnCode.ERROR.getCode());
            returnData.put("returnData", ReturnCode.ERROR.getMsg());
        }
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(returnData));
        pw.flush();
        pw.close();
    }

    // 更新游戏菜单信息
    private int updateGameInfo(HttpServletRequest req, HttpServletResponse resp) {
        Game game = new Game();
        game.setGid(Long.parseLong(req.getParameter("gid")));
        game.setName(req.getParameter("name"));
        game.setTitle(req.getParameter("title"));
        game.setSize(Double.parseDouble(req.getParameter("size")));
        game.setGameicon(req.getParameter("gameicon"));
        game.setDetailpic(req.getParameter("detailpic"));
        game.setIosurl(req.getParameter("iosurl"));
        game.setAndroidurl(req.getParameter("androidurl"));
        game.setTypename(req.getParameter("typename"));
        game.setType(req.getParameter("type"));
        game.setStatus(req.getParameter("status"));
        game.setPlatform(req.getParameter("platform"));
        game.setDesc(req.getParameter("desc"));
        return new GameServiceImpl().updateGameInfo(game);
    }

    // 通过id删除菜单信息
    private int deleteGameInfo(HttpServletRequest req, HttpServletResponse resp) {
        String gid = req.getParameter("gid");
        return new GameServiceImpl().deleteGameInfo(gid);
    }

    // 新增游戏菜单信息
    private int insertGameInfo(HttpServletRequest req, HttpServletResponse resp) {
        Game game = new Game();
        game.setName(req.getParameter("name"));
        game.setTitle(req.getParameter("title"));
        game.setSize(Double.parseDouble(req.getParameter("size")));
        game.setGameicon(req.getParameter("gameicon"));
        game.setDetailpic(req.getParameter("detailpic"));
        game.setIosurl(req.getParameter("iosurl"));
        game.setAndroidurl(req.getParameter("androidurl"));
        game.setTypename(req.getParameter("typename"));
        game.setType(req.getParameter("type"));
        game.setStatus(req.getParameter("status"));
        game.setPlatform(req.getParameter("platform"));
        game.setDesc(req.getParameter("desc"));
        return new GameServiceImpl().insertGameInfo(game);
    }
}
